package client.comm.baoding.ui.vm


import android.app.Application
import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import client.comm.commlib.network.data.JsonResult
import client.comm.commlib.network.data.Ret
import client.comm.commlib.toast.toast
import client.comm.baoding.ui.viewmodel.MiddleVm

class AddAddrViewModel(app: Application) : MiddleVm(app) {
    val name = MutableLiveData<String>("")
    val id = MutableLiveData<String>("")
    val phone = MutableLiveData<String>("")

    val qu = MutableLiveData<String>("")
    val quIds = MutableLiveData<String>("")
    val detail = MutableLiveData<String>("")
    val isShowing = MutableLiveData<Boolean>(false)
    val call = MutableLiveData<JsonResult<Ret>>()

    fun commit() {
        if (TextUtils.isEmpty(name.value) ||
            TextUtils.isEmpty(phone.value) ||
            TextUtils.isEmpty(qu.value) ||
            TextUtils.isEmpty(detail.value)
        ) {
            toast("所有不得为空")
            return
        }

        hold({
            val quIds = qu.value!!.split(" ")

//            val sheng = Util.getCityById(getApplication(), quIds[0])
//            val shi = Util.getCityById(getApplication(), quIds[1])
//            val qu = Util.getCityById(getApplication(), quIds[2])

            val sheng = quIds[0]
            val shi = quIds[1]
            val qu = quIds[2]
            server.set_user_address(
                id.value!!,
                name.value!!,
                phone.value!!,
                sheng,
                shi,
                qu,
                detail.value!!
            )
        }, {
            isShowing.value = true
        }, {
            call.value = it
        }, {

        }, {
            isShowing.value = false
        })
    }
}